<?php

namespace app\api\model\kuyou\applet;

use app\api\controller\kuyou\applet\Commons;
use think\Db;
use think\Model;


class Prefix extends Model
{

    // 表名
    protected $name = 'kuyou_prefix';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;

    /**
     * 获取设置
     * @param string $name
     * @return array
     */
    public static function getPrefix($type = null)
    {
        $list = collection(self::where(function ($query) use ($type) {
            if (!is_null($type)) {
                if(strpos($type,',') !== false){
                    $type = explode(',',$type);
                    $query->where('type', 'in', $type);
                }else{
                    $query->where('type', '=', $type);
                }
            }
        })->field('id,type,name,prefix,year,month,day,hour,minute,second,start,strlen,example')->select())->toArray();
        return $list;
    }

    /**
     * 获取设置
     * @param array $set
     * @return array
     */
    public static function setPrefix($set){
        if(empty($set)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $data = [];
            foreach ($set as $set_k=>$set_v){
                $check = (new Commons())->checkParameter($set_v,[
                    'type|单号类型'=>'require|number',
                    'prefix|单号前缀'=>'require|letter',
                    'year|年'=>'require|in:1,2',
                    'month|月'=>'require|in:1,2',
                    'day|日'=>'require|in:1,2',
                    'hour|时'=>'require|in:1,2',
                    'minute|分'=>'require|in:1,2',
                    'second|秒'=>'require|in:1,2',
                    'start|起始数'=>'require|number',
                    'strlen|自定义数字长度'=>'require|number',
                ]);
                if(isset($check['status'])){
                    return $check;
                }else{
                    $type = $set_v['type'];
                    $row = self::where(function ($query) use ($type) {
                        $query->where('type', '=', $type);
                    })->find();
                    if(empty($row)){
                        return ['status'=>405,'msg'=>'更新的设置不存在'];
                    }else{

                        $datas = [
                            'type' => $set_v['type'],
                            'prefix' => $set_v['prefix'],
                            'year' => isset($set_v['year'])?$set_v['year']:2,
                            'month' => isset($set_v['month'])?$set_v['month']:2,
                            'day' => isset($set_v['day'])?$set_v['day']:2,
                            'hour' => isset($set_v['hour'])?$set_v['hour']:2,
                            'minute' => isset($set_v['minute'])?$set_v['minute']:2,
                            'second' => isset($set_v['second'])?$set_v['second']:2,
                            'start' => isset($set_v['start'])?$set_v['start']:1,
                            'strlen' => isset($set_v['strlen'])?$set_v['strlen']:1,
                        ];
                        $row = Db::name('kuyou_prefix')->where(['type'=>$set_v['type']])->find();
                        foreach ($row as $row_k=>$row_v){
                            if(isset($datas[$row_k]) && $datas[$row_k]!=$row_v){
                                $order = Db::name($row['dbname'])->find();
                                if($order){
                                    return ['status'=>400,'msg'=>'已产生'.$row['name'].'数据，不可再修改'.$row['name'].'单号组合规则'];
                                }
                            }
                        }

                        $example = trim($set_v['prefix']);
                        if($set_v['year']==1){
                            $example .= date('Y');
                        }
                        if($set_v['month']==1){
                            $month = date('m');
                            $example .= $month<10?'0'.$month:$month;
                        }
                        if($set_v['day']==1){
                            $day = date('d');
                            $example .= $day<10?'0'.$day:$day;
                        }
                        if($set_v['hour']==1){
                            $hour = date('H');
                            $example .= $hour<10?'0'.$hour:$hour;
                        }
                        if($set_v['minute']==1){
                            $minute = date('i');
                            $example .= $minute<10?'0'.$minute:$minute;
                        }
                        if($set_v['second']==1){
                            $second = date('second');
                            $example .= $second<10?'0'.$second:$second;
                        }
                        $start = empty($set_v['start'])?1:$set_v['start'];
                        $strlen = empty($set_v['strlen'])?1:$set_v['strlen'];
                        if($strlen>10){
                            return ['status'=>400,'msg'=>'自定义数字长度不能大于10'];
                        }
                        for ($a=0;$a<$strlen-strlen(((string)$start));$a++){
                            $example .= '0';
                        }
                        $example .= $start;
                        $data[] = [
                            'id' => $row['id'],
                            'prefix' => $set_v['prefix'],
                            'year' => $set_v['year'],
                            'month' => $set_v['month'],
                            'day' => $set_v['day'],
                            'hour' => $set_v['hour'],
                            'minute' => $set_v['minute'],
                            'second' => $set_v['second'],
                            'start' => $set_v['start'],
                            'strlen' => $set_v['strlen'],
                            'example' => $example,
                        ];
                    }
                }
            }
            $res = (new Prefix())->saveAll($data);
            if($res){
                return ['status'=>200,'msg'=>'更新设置成功'];
            }else{
                return ['status'=>500,'msg'=>'更新设置失败'];
            }
        }
    }
}
